//
//  PhotoBrowserViewCell.swift
//  Status
//
//  Created by Kylin on 16/3/31.
//  Copyright © 2016年 Kylin. All rights reserved.
//

import UIKit
import SDWebImage

protocol PhotoBroserViewCellDelegate : NSObjectProtocol {
    func imageViewClickForCell(cell : PhotoBrowserViewCell)
}
class PhotoBrowserViewCell: UICollectionViewCell {
    
    // MARK:-  自定义属性
    var picURL : NSURL? {
        didSet {
            // 1.nil值校验
            guard let picURL = picURL else {
                return
            }
            
            setupUIContent(picURL)
        }
    }
    
    var delegate : PhotoBroserViewCellDelegate?
    // MARK:- 懒加载属性
    private lazy var scrollView : UIScrollView = UIScrollView()
    lazy var imageView : UIImageView = UIImageView()
    private lazy var progressView : PhotoBrowserProgressView = PhotoBrowserProgressView()
    
    // MARK:- 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
}

// MARK:- 设置UI内容
extension PhotoBrowserViewCell {
    private func setupUI(){
        
        // 1:添加子控件
        contentView.addSubview(scrollView)
        scrollView.addSubview(imageView)
        contentView.addSubview(progressView)
        
        // 2:设置frame
        scrollView.frame = contentView.bounds
        scrollView.frame.size.width -= 15
        progressView.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
        progressView.center = CGPoint(x: contentView.bounds.width * 0.5 - 7.5, y: contentView.bounds.height * 0.5)
        
        // 3.设置控件的属性
        progressView.hidden = true
        progressView.backgroundColor = UIColor.clearColor()
        
        // 4.图片的缩放
        scrollView.minimumZoomScale = 0.5
        scrollView.maximumZoomScale = 2.0
        scrollView.delegate = self
        
        // 5.给imageView添加手势
        imageView.userInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(PhotoBrowserViewCell.imageViewClick))
        imageView.addGestureRecognizer(tap)
        
    }
    
    @objc private func imageViewClick(){
        delegate?.imageViewClickForCell(self)
    }
}

// MARK:- 设置界面的内容
extension PhotoBrowserViewCell {
    private func setupUIContent(picURL : NSURL) {
        
        // 1:取出image
        guard let image = SDWebImageManager.sharedManager().imageCache.imageFromDiskCacheForKey(picURL.absoluteString) else {
            return
        }
        // 2.计算图片放大之后的高度
        let screenW = UIScreen.mainScreen().bounds.width
        let screenH = UIScreen.mainScreen().bounds.height
        let imageViewH = screenW / image.size.width * image.size.height
        
        // 3.设置imageView的frame
        imageView.frame = CGRect(x: 0, y: 0, width: screenW, height: imageViewH)
        // 4.设置内边距
        if imageViewH > screenH {
            scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        }else{
            let topInset = (screenH - imageViewH) * 0.5
            scrollView.contentInset = UIEdgeInsets(top: topInset, left: 0, bottom: 0, right: 0)
        }
        // 5.scrollView的contentSize
        scrollView.contentSize = CGSize(width: 0, height: imageViewH)
        
        // 6:下载大图
        let middleURL = getMiddlePicURL(picURL)
        
        // 6.1 设置中等图片
        progressView.hidden = false
        imageView.sd_setImageWithURL(middleURL, placeholderImage: image, options: [], progress: { (current , total) -> Void in
            self.progressView.progress = CGFloat(current) / CGFloat(total)
            
            }) { (_, _, _, _) -> Void in
                self.progressView.hidden = true
        }
        
    }
    
    private func getMiddlePicURL(smallURL : NSURL) -> NSURL {
         // 1.获取小图的urlString
        let smallURL = smallURL.absoluteString
        
        // 2.将缩略单词替换middle
        let bmiddleURLString = (smallURL as NSString).stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
        
        return NSURL(string: bmiddleURLString)!
        
    }
    
}

// MARK:- scrollView的代理方法
extension PhotoBrowserViewCell : UIScrollViewDelegate{
    
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        // 1.设置垂直方向
        var insetV = (UIScreen.mainScreen().bounds.height - view!.frame.height) * 0.5
        insetV = insetV < 0 ? 0 : insetV
        
        // 2.设置水平方向
        var insetH = (UIScreen.mainScreen().bounds.width - view!.frame.width) * 0.5
        insetH = insetH < 0 ? 0 : insetH
        
        // 3.设置内边距
        scrollView.contentInset = UIEdgeInsets(top: insetV, left: insetH, bottom: insetV, right: insetH)
        
    }
}













